/**
 * 
 */
package vn.edu.fpt.xml.hrm.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * @author Yoshimi
 * 
 */
public class FormatUtil {

    public static String formatDate(Date date, String pattern, Locale locale) {
        DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
        return dateFormat.format(date);
    }

    public static String formatDate(XMLGregorianCalendar date, String pattern, Locale locale) {
        DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
        return dateFormat.format(new Date(date.toGregorianCalendar().getTimeInMillis()));
    }

    public static Date stringToDate(String year, String month, String day) {
        return generateDate(Integer.parseInt(year), Integer.parseInt(month),
                Integer.parseInt(day));
    }

    public static Date stringToDate(String year, String month, String day,
            String hour, String minute, String second) {
        return generateDate(Integer.parseInt(year), Integer.parseInt(month),
                Integer.parseInt(day), Integer.parseInt(hour),
                Integer.parseInt(minute), Integer.parseInt(second));
    }

	/**
	 * Generate a <tt>java.util.Date</tt> object from the given string
	 * 
	 * @param date
	 *            the string to be parsed
	 * @param pattern
	 *            the date format pattern of the string
	 * @param locale
	 *            the locale of the date format in the string
	 * @return a <tt>java.util.Date</tt> object
	 * @throws ParseException
	 *             if the locale and pattern does not match the string format
	 * @author Yoshimi
	 */
	public static Date stringToDate(String date, String pattern, Locale locale)
			throws ParseException {
		DateFormat dateFormat = new SimpleDateFormat(pattern, locale);
		return dateFormat.parse(date);
	}
    
	/**
	 * Generate a <tt>java.util.Date</tt> object from the given string
	 * 
	 * @param date
	 *            the string to be parsed
	 * @param pattern
	 *            the date format pattern of the string
	 * @return a <tt>java.util.Date</tt> object
	 * @throws ParseException
	 *             if the default locale and pattern does not match the string
	 *             format
	 * @author Yoshimi
	 */
	public static Date stringToDate(String date, String pattern)
			throws ParseException {
		DateFormat dateFormat = new SimpleDateFormat(pattern,
				Locale.getDefault());
		return dateFormat.parse(date);
	}
    
	/**
	 * Generate a <tt>java.sql.Date</tt> object from the given string
	 * 
	 * @param date
	 *            the string to be parsed
	 * @param pattern
	 *            the date format pattern of the string
	 * @param locale
	 *            the locale of the date format in the string
	 * @return a <tt>java.sql.Date</tt> object
	 * @throws ParseException
	 *             if the locale and pattern does not match the string format
	 * @author Yoshimi
	 */
	public static java.sql.Date stringToSqlDate(String date, String pattern,
			Locale locale) throws ParseException {
		return dateToDate(stringToDate(date, pattern, locale));
	}

    
	/**
	 * Generate a <tt>java.sql.Date</tt> object from the given string
	 * 
	 * @param date
	 *            the string to be parsed
	 * @param pattern
	 *            the date format pattern of the string
	 * @return a <tt>java.sql.Date</tt> object
	 * @throws ParseException
	 *             if the default locale and pattern does not match the string
	 *             format
	 * @author Yoshimi
	 */
	public static java.sql.Date stringToSqlDate(String date, String pattern)
			throws ParseException {
		return dateToDate(stringToDate(date, pattern));
	}


    public static Date generateDate(int year, int month, int day) {
        return generateDate(year, month, day, 0, 0, 0);
    }

    public static Date generateDate(int year, int month, int day, int hour,
            int minute, int second) {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(year, month - 1, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

    public static Date dateToDate(java.sql.Date arg) {
        Date date = null;
        date = new Date();
        date.setTime(arg.getTime());
        return date;
    }

    public static XMLGregorianCalendar dateToCalendar(java.sql.Date arg) throws DatatypeConfigurationException {
        XMLGregorianCalendar result = null;
        GregorianCalendar calendar = null;
        calendar = new GregorianCalendar();
        calendar.setTimeInMillis(arg.getTime());
        result = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        return result;
    }

    public static java.sql.Date calendarToDate(XMLGregorianCalendar arg) {
        java.sql.Date date = null;
        date = new java.sql.Date(arg.toGregorianCalendar().getTimeInMillis());
        return date;
    }

    public static java.sql.Date dateToDate(Date arg) {
        java.sql.Date date = null;
        date = new java.sql.Date(arg.getTime());
        return date;
    }

    public static int[] stringToInt(String[] args) {
        int[] results = null;
        int size = 0;
        if (args != null) {
            size = args.length;
            results = new int[size];
            for (int i = 0; i < size; i++) {
                results[i] = Integer.parseInt(args[i]);
            }
        }
        return results;
    }

    public static String convertDatetoStringMMDDYYYY(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        return formatter.format(date);
    }

    public static Date convertStringtoDateMMDDYYYY(String date) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            return formatter.parse(date);
        } catch (ParseException ex) {
            return null;
        }
    }

    public static void main(String[] args) {
        FormatUtil.stringToDate("1980", "10", "20", "14", "03", "27");
    }
}
